<?php

namespace App\Services\UserInviteInfo;

use App\Models\User;
use App\Models\Product;
use Illuminate\Support\Str;
use Intervention\Image\Gd\Font;
use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class MakeProductInvitePoster
{
	/**
	 * @var User
	 */
	protected $user;
	
	/**
	 * @var Product
	 */
	protected $product;
	
	protected $channel = 'wap';
	
	public function __construct(User $user, Product $product, $channel = 'wap')
	{
		$this->user = $user;
		$this->product = $product;
		$this->channel = $channel;
	}
	
	protected function getPosterName()
	{
		return "poster-user-{$this->user->id}-product-{$this->product->id}.png";
	}
	
	protected function getRemotePosterDir()
	{
		return 'images/mini-product-invite-posters';
	}
	
	protected function getRemotePosterSavePath()
	{
		return $this->getRemotePosterDir() . '/' . $this->getPosterName();
	}
	
	protected function getLocalPosterSavePath(string $name)
	{
		$localPath = public_path('temp/mini-product-invite-posters');
		if (! File::isDirectory($localPath)) {
			File::makeDirectory($localPath, 755, true, true);
		}
		return "{$localPath}/{$name}.png";
	}
	
	public function make()
	{
		$product = $this->product;
		
		/*if (Storage::exists($this->getRemotePosterSavePath())) {
			return Storage::url($this->getRemotePosterSavePath());
		}*/
		
		$posterDir = $this->getRemotePosterDir();
		$posterName = $this->getPosterName();
		
		$logo = Image::make(oss('images/mini/product/logo.png'))->resize(273, 119);

		$cover = Image::make(img_path($product->cover))->resize(662, 662);
		
		$ewm = Image::make(oss('images/mini/product/cysbewm.png'))->resize(20, 155);
		
		$finalPrice = big_decimal($product->price_lowest);
		
		// make qrcode.
		$qrcode = Image::make($this->user->inviteQrcode($product))->resize(157, 157);

		$avatar = $this->user->avatar ? img_path($this->user->avatar) : oss('images/wap/ttwrt_common_logo.png');

		$avatar = Image::make($avatar)->resize(67, 67);

		$title = mb_substr($product->title,0,14,'utf-8').'...';

		$name = $this->user->name."的推荐";
		
		Image::canvas(750, 1250, '#FFFFFF')
			->insert($logo, 'top', 239, 73)
			->insert($cover, 'top', 44, 232)
			->insert($avatar, 'top-left', 43, 925)

			->text($name,130, 969, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#000000');
				$font->size(30);
			})
			->text($title,43,1047, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#000000');
				$font->size(30);
			})
			->text("自购佣金：￥".$product->skus->first()->own_rewards, 42, 1098, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#FF4758');
				$font->size(24);
			})
			->text("会员价：￥".$finalPrice, 43, 1160, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#858585');
				$font->size(38);
			})


			->insert($ewm, 'bottom-right', 209, 71)
			->insert($qrcode, 'bottom-right', 42, 70)
			->save($this->getLocalPosterSavePath($posterName));
		
		// move to oss.
		Storage::putFileAs($posterDir, $this->getLocalPosterSavePath($posterName), $posterName);
		
		// remove local poster.
		File::delete($this->getLocalPosterSavePath($posterName));
		
		return Storage::url("{$posterDir}/{$posterName}");
	}
}
